package Message;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class TestEncoder extends ProtocolEncoderAdapter{

	public final Charset charset;
	
	public TestEncoder (Charset charset)
	{
		this.charset = charset;
	}
	
	@Override
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
			throws Exception {
		// TODO Auto-generated method stub
		ConnectionMessage msg = (ConnectionMessage) message;
		CharsetEncoder ce = charset.newEncoder();
		IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);
		String statusLine = "M sip:wap.fetion.com.cn SIP-C/2.0";
		String sender = msg.getSender();
		String receiver = msg.getReceiver();
		String smscontent = msg.getMessage();
		buffer.putString(statusLine+'\n', ce);
		buffer.putString("S: " + sender + '\n', ce);
		buffer.putString("R: " + receiver + '\n', ce);
		buffer.putString("L: " + (smscontent.getBytes(charset).length)+ "\n",ce);
		buffer.putString(smscontent, ce);
		buffer.flip();
		out.write(buffer);
		
	}
	

}
